package com.miya.demo.entity;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.monitor4all.logRecord.bean.LogDTO;
import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.JdbcType;

import java.time.LocalDateTime;

@Data
@TableName(value = "t_log_record")
@NoArgsConstructor
@AllArgsConstructor
public class LogRecord {

    /**
     * 主键id
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 日志id
     */
    @TableField(value = "log_id")
    private String logId;

    /**
     * 业务唯一ID
     */
    @TableField(value = "biz_id")
    private Long bizId;

    /**
     * 业务类型
     */
    @TableField(value = "biz_type")
    private String bizType;

    /**
     * 函数执行失败时写入异常信息
     */
    @TableField(value = "`exception`")
    private String exception;

    /**
     * 操作执行时间
     */
    @TableField(value = "operate_date")
    private LocalDateTime operateDate;

    /**
     * 函数是否执行成功: 0-失败; 1-成功
     */
    @TableField(value = "success")
    private Boolean success;

    /**
     * 日志内容
     */
    @TableField(value = "msg")
    private String msg;

    /**
     * 自定义标签
     */
    @TableField(value = "tag")
    private String tag;

    /**
     * 方法执行成功后的返回值（字符串或JSON化实体）
     */
    @TableField(value = "return_str")
    private String returnStr;

    /**
     * 方法执行耗时（单位：毫秒）
     */
    @TableField(value = "execution_time")
    private Long executionTime;

    /**
     * 额外信息
     */
    @TableField(value = "extra")
    private String extra;

    /**
     * 操作人ID
     */
    @TableField(value = "operator_id")
    private Long operatorId;

    /**
     * 实体类对象Diff数据，包括变更的字段名，字段值，类名等
     */
    @TableField(value = "diff_dto_List")
    private String diffDtoList;

    /**
     * 删除标识: 0-未删除; 1-已删除
     */
    @TableField(value = "delete_status")
    private Boolean deleteStatus;

    /**
     * 创建人
     */
    @TableField(value = "create_by")
    private Long createBy;

    /**
     * 创建时间
     */
    @TableField(value = "create_time")
    private LocalDateTime createTime;

    /**
     * 更新人
     */
    @TableField(value = "update_by")
    private Long updateBy;

    /**
     * 更新时间
     */
    @TableField(value = "update_time")
    private LocalDateTime updateTime;

    public LogRecord(LogDTO logDTO) {
        this.bizId = Convert.toLong(logDTO.getBizId());
        this.bizType = logDTO.getBizType();
        this.diffDtoList = CollUtil.isNotEmpty(logDTO.getDiffDTOList()) ? JSONUtil.toJsonStr(logDTO.getDiffDTOList()) : StrUtil.EMPTY;
        this.operateDate = LocalDateTimeUtil.of(logDTO.getOperateDate());
        this.logId = logDTO.getLogId();
        this.exception = logDTO.getException();
        this.executionTime = logDTO.getExecutionTime();
        this.extra = logDTO.getExtra();
        this.msg = logDTO.getMsg();
        this.returnStr = logDTO.getReturnStr();
        this.success = logDTO.getSuccess();
        this.tag = logDTO.getTag();
        this.operatorId = Convert.toLong(logDTO.getOperatorId());
        this.createBy = Convert.toLong(logDTO.getOperatorId());
        this.updateBy = Convert.toLong(logDTO.getOperatorId());
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.deleteStatus = Boolean.FALSE;
    }
}